import sys
import traceback

import Ice

import ice.common as common
from ice.auth import passport


# noinspection PyUnresolvedReferences
class PassportServiceImpl(passport.LoginService):
    """
    用户认证服务
    """

    @staticmethod
    def login(userName, passport, userType, current=None):
        """
        用户登录接口
        :param userName: 登录用户的用户名
        :param passport: 登录用户的用户密码
        :param userType: 登录用户的用户类型
        :param current: rpc对象
        :return: 如果登录成功，那么返回值为true，否则返回值为false
        """
        print("login name = " + userName)
        print("passport = " + passport)
        print("userType = " + str(userType))
        from models.core_base.user_system import User

        # 登录判断及后台处理逻辑
        # 1、判断当前用户是否已经登录，如果已经登录，那么直接返回当true
        # 2、用户没有登录，进入登录逻辑，判断当前用户的用户名密码是否在数据库中能够获得
        # 2.1获取到用户信息，返回true
        # 2.2无法获取用户信息，返回false
        try:
            loginUserInfo = User.objects.get(login_id=userName, password=passport)
            print("查找到用户：%s" % loginUserInfo)
            return common.RetObj("id_1", code=1, msg=str(loginUserInfo.user_id), subCode=1)
        except User.DoesNotExist:
            # 无法根据提供的用户名和密码获得用户信息
            return common.RetObj("id_1", code=0, msg="登录失败", subCode=1)

    @staticmethod
    def thirdLogin(openId, thirdType, userType, current=None):
        """
        第三方登录接口
        :param openId: 第三方登录后返回的第三方帐号凭证 
        :param thirdType: 第三方帐号类型
        :param userType: 用户类型
        :param current: rpc对象
        :return: 如果登录成功，那么返回值为true，否则返回值为false
        """
        pass

    @staticmethod
    def checkStatus(userName, userType, current=None):
        """
        检查用户的登录状态
        :param userName: 用户名
        :param userType: 用户类型
        :param current: rpc对象
        :return: 返回当前用户的状态
        """
        pass


if __name__ == '__main__':
    from bin.service_runner import execute

    execute()
    status = 0
    ic = None
    try:
        ic = Ice.initialize(sys.argv)
        adapter = ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000")
        obj = PassportServiceImpl()
        adapter.add(obj, ic.stringToIdentity("SimplePrinter"))
        adapter.activate()
        ic.waitForShutdown()
    except:
        traceback.print_exc()
        status = 1

    if ic:
        # Clean up
        try:
            ic.destroy()
        except:
            traceback.print_exc()
            status = 1

    sys.exit(status)
